#include <synSubControllItem.h>
#include <postManager.h>

namespace View {

  bool SynAttackButton::OnClicked()
  {
    if(true == buttonType && !(synControll->CheckInputData()) )
      return false;

    if(true == buttonType)
      synControll->SendAllAddress();
    
    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_ATTACK_STAT);
    poster->SetDoubleData(Model::SYN_ATTACK);
    poster->SetBoolData(buttonType);
    poster->SendBroadcast();
    return true;
  }

  SynSubControllItem::SynSubControllItem(GooCanvasItem *parent)
    :SubControllItem(parent),
     targetIpEntry(gtk_entry_new()),
     targetPortEntry(gtk_entry_new()),
     srcIpEntry(gtk_entry_new())
  {
    ViewConf *conf = ViewConf::GetViewConf();
    double synEntryX(conf->GetDoubleData("synEntryX")),
      synEntryY(conf->GetDoubleData("synEntryY")),
      synEntryHigh(conf->GetDoubleData("synEntryHigh")),
      synEntryLength(conf->GetDoubleData("synEntryLength")),
      synMessageX(conf->GetDoubleData("synMessageX")),
      synMessageY(conf->GetDoubleData("synMessageY")),
      synMessageL(conf->GetDoubleData("synMessageL")),
      synLabelL(conf->GetDoubleData("synLabelL")),
      tmpEntryY(synEntryY);
    std::string bgPath,mgPath,fgPath;
    GdkPixbuf *bg,*mg,*fg;
    bgPath = VIEW_CONF_PATH +
      static_cast<const std::string>("skin/default/") + 
      "synSubItemBg.png";
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    goo_canvas_image_new(root,bg,conf->GetDoubleData("SubControllItemBgX"),
			 conf->GetDoubleData("SubControllItemBgY"),NULL);
    g_object_unref(bg);

    std::string font("Sans 17");
    std::string color("#591D5D");
    goo_canvas_widget_new(root,targetIpEntry,synEntryX,tmpEntryY,synEntryLength,synEntryHigh,NULL);
    targetIpLabel = goo_canvas_text_new(root,conf->GetStringData("synTargetIp").c_str(),
					synEntryX - synLabelL,tmpEntryY,
					synLabelL,GTK_ANCHOR_NW,
					"font", font.c_str(),"fill_color",
					color.c_str(),NULL);
    tmpEntryY += synEntryHigh*2.0;
    goo_canvas_widget_new(root,targetPortEntry,synEntryX,tmpEntryY,synEntryLength,synEntryHigh,NULL);
    targetPortLabel = goo_canvas_text_new(root,conf->GetStringData("synTargetPort").c_str(),
					  synEntryX - synLabelL,tmpEntryY,
					  synLabelL,GTK_ANCHOR_NW,
					  "font", font.c_str(),"fill_color",
					  color.c_str(),NULL);
    tmpEntryY += synEntryHigh*2.0;
    goo_canvas_widget_new(root,srcIpEntry,synEntryX,tmpEntryY,synEntryLength,synEntryHigh,NULL);
    srcIpLabel = goo_canvas_text_new(root,conf->GetStringData("synBogusSrcIP").c_str(),
				     synEntryX - synLabelL,tmpEntryY,
				     synLabelL,GTK_ANCHOR_NW,
				     "font", font.c_str(),"fill_color",
				     color.c_str(),NULL);
    color = "#203a91";
    pleaseInputSynMessages = goo_canvas_text_new(root,conf->GetStringData("pleaseInputSynMessages").c_str(),
						 synMessageX,synMessageY,
						 synMessageL,GTK_ANCHOR_NW,
						 "font", font.c_str(),"fill_color",
						 color.c_str(),NULL);
    //g_object_set(pleaseInputSynMessages,"text","haha",NULL);


    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "starAttackBg.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackMg.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "starAttackFg.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    startAttackButton = new SynAttackButton(root,conf->GetDoubleData("synAttackButtonX"),
					    conf->GetDoubleData("synAttackButtonY"),
					    bg,mg,fg,true,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);
    
    bgPath = VIEW_CONF_PATH + static_cast<const std::string>("skin/default/") +
      "pauseAttackBgpng.png" ;
    mgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackMgpng.png" ;
    fgPath = VIEW_CONF_PATH +static_cast<const std::string>("skin/default/") +
      "pauseAttackFgpng.png" ;
    bg = gdk_pixbuf_new_from_file(bgPath.c_str(),NULL);
    mg = gdk_pixbuf_new_from_file(mgPath.c_str(),NULL);
    fg = gdk_pixbuf_new_from_file(fgPath.c_str(),NULL);
    stopAttackButton = new SynAttackButton(root,conf->GetDoubleData("synAttackButtonX"),
					   conf->GetDoubleData("synAttackButtonY"),
					   bg,mg,fg,false,this);
    g_object_unref(bg);
    g_object_unref(mg);
    g_object_unref(fg);
    stopAttackButton->Hide();

  }

  SynSubControllItem::~SynSubControllItem()
  {

  }

  int SynSubControllItem::OnReceive(Controller::Broadcast *broadcast)
  {
    using namespace Controller;
    BroadcastType type = broadcast->GetBroadcastType();
    switch(type)
      {
      case BROADCAST_TYPE_ATTACK_STAT:{
        if(true == broadcast->GetBoolData() &&
           Model::SYN_ATTACK == broadcast->GetDoubleData() ){
          startAttackButton->Hide();
          stopAttackButton->Show();
        }else if(false == broadcast->GetBoolData() &&
		 Model::SYN_ATTACK == broadcast->GetDoubleData() ){
          stopAttackButton->Hide();
          startAttackButton->Show();
        }
      }break;
      case BROADCAST_TYPE_CHANGE_LANGUAGE:{
	ViewConf *conf = ViewConf::GetViewConf();
	g_object_set(pleaseInputSynMessages,"text",conf->GetStringData("pleaseInputSynMessages").c_str(),NULL);
	g_object_set(targetIpLabel,"text",conf->GetStringData("synTargetIp").c_str(),NULL);
	g_object_set(targetPortLabel,"text",conf->GetStringData("synTargetPort").c_str(),NULL);
	g_object_set(srcIpLabel,"text",conf->GetStringData("synBogusSrcIP").c_str(),NULL);
      }break;
      default:return 1;
      }
    return 0;
  }

  bool SynSubControllItem::SendAllAddress()
  {
    std::string targetIP(gtk_entry_get_text((GtkEntry*)targetIpEntry)),
      targetPort(gtk_entry_get_text((GtkEntry*)targetPortEntry)),
      srcIp(gtk_entry_get_text((GtkEntry*)srcIpEntry)),
      allAddress(targetIP + "#" + targetPort + "#" + srcIp);

    Controller::PostManager *poster = Controller::PostManager::GetPostManager();
    poster->SetBroadcastType(Controller::BROADCAST_TYPE_SYN_ALL_ADDRESS);
    poster->SetStringData(allAddress);
    poster->SendBroadcast();
    return true;
  }

  bool SynSubControllItem::CheckInputData()
  {
    std::string targetIp(gtk_entry_get_text((GtkEntry*)targetIpEntry)),
      targetPort(gtk_entry_get_text((GtkEntry*)targetPortEntry)),
      srcIp(gtk_entry_get_text((GtkEntry*)srcIpEntry));

    NetAddressChecker *checker = new NetAddressChecker();
    ViewConf *conf = ViewConf::GetViewConf();
    MessageDialog *dialog = MessageDialog::GetMessageDialog();
    dialog->SetTitle(conf->GetStringData("synDialogTitle"));

    if(!checker->CheckIP(targetIp))
      dialog->SetText(conf->GetStringData("synTargetIPWrong"));
    else if(!checker->CheckPort(targetPort))
      dialog->SetText(conf->GetStringData("synTargetPortWrong"));
    else if(!checker->CheckIP(srcIp))
      dialog->SetText(conf->GetStringData("synSrcIP"));
    else
      return true;

    delete checker;
    dialog->Show();
    return false;
  }

}
